var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var app_utils = require("../utils/app_utils");

var xigua_read = {
  go_page_from_home(count) {
    var logger = utils.create_logger("read go_page_from_home: ");
    sleep(random(3, 5) * 1000);
    logger("SUCCESS");
    var swipe_times = utils.get_random_interval(1, 0, 1, 1);
    logger("下面，将滑动--" + swipe_times + "--次");
    while (swipe_times > 0) {
      if (ocr_utils.screen_exists_text_one(["直播间", "上滑继续"], false)) {
        logger("找到--直播间或上滑继续--，直接跳过");
        utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
        swipe_times = swipe_times - 1;
        continue;
      }
      utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      swipe_times = swipe_times - 1;
      logger("还需要滑动--" + swipe_times + "--次");
    }
    this.init(count);
  },
  init(count) {
    console.log("read init: ");
    this.run(count);
  },
  run(count) {
    var logger = utils.create_logger("read run: ");
    for (var index = 0; index < count; index++) {
      utils.swipe(1 / 2, (1 / 10) * 6, 1 / 2, (1 / 10) * 2);
      sleep(utils.get_random_interval(2, 0, 1, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one(["直播中"], false);
      if (ocr_text) {
        logger("找到-xx中-，跳过当前视频");
        index = index - 1;
        continue;
      }
      var video_interval = utils.get_random_interval(60, 0, 30, 1);
      logger(
        "第-" +
          (index + 1) +
          "-次滑动已经完成，下面将开始看视频-" +
          video_interval +
          "-秒"
      );
      this.sleep_and_check(video_interval);
    }
    this.after();
  },
  sleep_and_check(video_interval) {
    var logger = utils.create_logger("read sleep_and_check: ");
    var check_interval = 20;
    while (video_interval > check_interval) {
      sleep(utils.get_random_interval(check_interval, 0, 1, 1000));
      if (ocr_utils.screen_exists_text_one(["重播", "快关注下吧"], false)) {
        logger("找到-重播或快关注下吧-，当前视频已经结束");
        return;
      }
      video_interval = video_interval - check_interval;
      if (video_interval > check_interval) {
        logger("当前视频还剩-" + video_interval + "-秒");
      }
    }
    if (video_interval > 0) {
      logger("还剩最后的-" + video_interval + "-秒");
      sleep(video_interval * 1000);
    }
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};
function xigua(count) {
  var app_name = "西瓜视频";
  if (app_utils.openApp(app_name)) {
    xigua_read.go_page_from_home(random(count, count + 3));
    app_utils.back_close(app_name);
  }
}

var youshi_read = {
  go_page_from_home(count) {
    var logger = utils.create_logger("read go_page_from_home: ");
    sleep(random(5, 7) * 1000);
    logger("SUCCESS");
    utils.click_point.apply(
      utils,
      utils.get_size_point([216, 2153, 432, 2274], 1080, 2340)
    );
    sleep(random(2, 3) * 1000);
    var swipe_times = utils.get_random_interval(1, 0, 1, 1);
    logger("下面，将滑动--" + swipe_times + "--次");
    while (swipe_times > 0) {
      if (ocr_utils.screen_exists_text_one(["直播间", "上滑继续"], false)) {
        logger("找到--直播间或上滑继续--，直接跳过");
        utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
        swipe_times = swipe_times - 1;
        continue;
      }
      utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      swipe_times = swipe_times - 1;
      logger("还需要滑动--" + swipe_times + "--次");
    }
    this.init(count);
  },
  init(count) {
    console.log("read init: ");
    this.run(count);
  },
  run(count) {
    var logger = utils.create_logger("read run: ");
    for (var index = 0; index < count; index++) {
      utils.swipe(1 / 2, (1 / 10) * 6, 1 / 2, (1 / 10) * 2);
      sleep(utils.get_random_interval(2, 0, 1, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one(["直播中"], false);
      if (ocr_text) {
        logger("找到-xx中-，跳过当前视频");
        index = index - 1;
        continue;
      }
      var video_interval = utils.get_random_interval(10, 0, 20, 1);
      logger(
        "第-" +
          (index + 1) +
          "-次滑动已经完成，下面将开始看视频-" +
          video_interval +
          "-秒"
      );
      this.sleep_and_check(video_interval);
    }
    this.after();
  },
  sleep_and_check(video_interval) {
    var logger = utils.create_logger("read sleep_and_check: ");
    var check_interval = 20;
    while (video_interval > check_interval) {
      sleep(utils.get_random_interval(check_interval, 0, 1, 1000));
      if (ocr_utils.screen_exists_text_one(["重播", "快关注下吧"], false)) {
        logger("找到-重播或快关注下吧-，当前视频已经结束");
        return;
      }
      video_interval = video_interval - check_interval;
      if (video_interval > check_interval) {
        logger("当前视频还剩-" + video_interval + "-秒");
      }
    }
    if (video_interval > 0) {
      logger("还剩最后的-" + video_interval + "-秒");
      sleep(video_interval * 1000);
    }
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};
function youshi(count) {
  var app_name = "有柿";
  if (app_utils.openApp(app_name)) {
    youshi_read.go_page_from_home(random(count, count + 3));
    app_utils.back_close(app_name);
  }
}

var yy_read = {
  go_page_from_home(count) {
    var logger = utils.create_logger("read go_page_from_home: ");
    sleep(random(5, 7) * 1000);
    logger("SUCCESS");
    utils.click_point.apply(
      utils,
      utils.get_size_point([41, 453, 529, 819], 1080, 2340)
    );
    sleep(random(2, 3) * 1000);
    utils.click_point.apply(
      utils,
      utils.get_size_point([41, 453, 529, 819], 1080, 2340)
    );
    sleep(random(2, 3) * 1000);
    this.init(count);
  },
  init(count) {
    console.log("read init: ");
    this.run(count);
  },
  run(count) {
    var logger = utils.create_logger("read run: ");
    for (var index = 0; index < count; index++) {
      var video_interval = utils.get_random_interval(30, 0, 30, 1);
      logger("第-" + (index + 1) + "-次看直播，预计-" + video_interval + "-秒");
      sleep(video_interval * 1000);
    }
    this.after();
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};

function yy(count) {
  var app_name = "YY";
  if (app_utils.openApp(app_name)) {
    yy_read.go_page_from_home(random(count, count + 3));
    app_utils.back_close(app_name);
  }
}

var toutiao_read = {
  go_page_from_home(count) {
    var logger = utils.create_logger("read go_page_from_home: ");
    sleep(random(5, 7) * 1000);
    logger("SUCCESS");
    utils.click_point.apply(
      utils,
      utils.get_size_point([216, 2175, 432, 2296], 1080, 2340)
    );
    sleep(random(2, 3) * 1000);
    var swipe_times = utils.get_random_interval(1, 0, 1, 1);
    logger("下面，将滑动--" + swipe_times + "--次");
    while (swipe_times > 0) {
      if (ocr_utils.screen_exists_text_one(["直播间", "上滑继续"], false)) {
        logger("找到--直播间或上滑继续--，直接跳过");
        utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
        swipe_times = swipe_times - 1;
        continue;
      }
      utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      swipe_times = swipe_times - 1;
      logger("还需要滑动--" + swipe_times + "--次");
    }
    this.init(count);
  },
  init(count) {
    console.log("read init: ");
    this.run(count);
  },
  run(count) {
    var logger = utils.create_logger("read run: ");
    for (var index = 0; index < count; index++) {
      utils.swipe(1 / 2, (1 / 10) * 6, 1 / 2, (1 / 10) * 2);
      sleep(utils.get_random_interval(2, 0, 1, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one(["直播中"], false);
      if (ocr_text) {
        logger("找到-xx中-，跳过当前视频");
        index = index - 1;
        continue;
      }
      var video_interval = utils.get_random_interval(10, 0, 20, 1);
      logger(
        "第-" +
          (index + 1) +
          "-次滑动已经完成，下面将开始看视频-" +
          video_interval +
          "-秒"
      );
      this.sleep_and_check(video_interval);
    }
    this.after();
  },
  sleep_and_check(video_interval) {
    var logger = utils.create_logger("read sleep_and_check: ");
    var check_interval = 20;
    while (video_interval > check_interval) {
      sleep(utils.get_random_interval(check_interval, 0, 1, 1000));
      if (ocr_utils.screen_exists_text_one(["重播", "快关注下吧"], false)) {
        logger("找到-重播或快关注下吧-，当前视频已经结束");
        return;
      }
      video_interval = video_interval - check_interval;
      if (video_interval > check_interval) {
        logger("当前视频还剩-" + video_interval + "-秒");
      }
    }
    if (video_interval > 0) {
      logger("还剩最后的-" + video_interval + "-秒");
      sleep(video_interval * 1000);
    }
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};

function toutiao(count) {
  var app_name = "今日头条";
  if (app_utils.openApp(app_name)) {
    toutiao_read.go_page_from_home(random(count, count + 3));
    app_utils.back_close(app_name);
  }
}

var shandian_read = {
  go_page_from_home(count) {
    var logger = utils.create_logger("read go_page_from_home: ");
    sleep(random(5, 7) * 1000);
    logger("SUCCESS");
    utils.click_point.apply(
      utils,
      utils.get_size_point([216, 2179, 432, 2291], 1080, 2340)
    );
    sleep(random(2, 3) * 1000);
    var swipe_times = utils.get_random_interval(1, 0, 1, 1);
    logger("下面，将滑动--" + swipe_times + "--次");
    while (swipe_times > 0) {
      if (ocr_utils.screen_exists_text_one(["直播间", "上滑继续"], false)) {
        logger("找到--直播间或上滑继续--，直接跳过");
        utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
        swipe_times = swipe_times - 1;
        continue;
      }
      utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      swipe_times = swipe_times - 1;
      logger("还需要滑动--" + swipe_times + "--次");
    }
    this.init(count);
  },
  init(count) {
    console.log("read init: ");
    this.run(count);
  },
  run(count) {
    var logger = utils.create_logger("read run: ");
    for (var index = 0; index < count; index++) {
      utils.swipe(1 / 2, (1 / 10) * 6, 1 / 2, (1 / 10) * 2);
      sleep(utils.get_random_interval(2, 0, 1, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one(["直播中"], false);
      if (ocr_text) {
        logger("找到-xx中-，跳过当前视频");
        index = index - 1;
        continue;
      }
      var video_interval = utils.get_random_interval(10, 0, 20, 1);
      logger(
        "第-" +
          (index + 1) +
          "-次滑动已经完成，下面将开始看视频-" +
          video_interval +
          "-秒"
      );
      this.sleep_and_check(video_interval);
    }
    this.after();
  },
  sleep_and_check(video_interval) {
    var logger = utils.create_logger("read sleep_and_check: ");
    var check_interval = 20;
    while (video_interval > check_interval) {
      sleep(utils.get_random_interval(check_interval, 0, 1, 1000));
      if (ocr_utils.screen_exists_text_one(["重播", "快关注下吧"], false)) {
        logger("找到-重播或快关注下吧-，当前视频已经结束");
        return;
      }
      video_interval = video_interval - check_interval;
      if (video_interval > check_interval) {
        logger("当前视频还剩-" + video_interval + "-秒");
      }
    }
    if (video_interval > 0) {
      logger("还剩最后的-" + video_interval + "-秒");
      sleep(video_interval * 1000);
    }
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};
function shandian(count) {
  var app_name = "闪电搜索";
  if (app_utils.openApp(app_name)) {
    shandian_read.go_page_from_home(random(count, count + 3));
    app_utils.back_close(app_name);
  }
}

function main() {
  for (var index = 0; index < 100; index++) {
    yy(2);
    toutiao(2);
    xigua(5);
    youshi(3);
    shandian(2);
  }
}

module.exports = main;
